package com.zhen.aikan.controller;

import com.zhen.aikan.entity.User;
import com.zhen.aikan.service.IUserService;
import com.zhen.aikan.vo.Result;
import com.zhen.aikan.vo.UserVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lijiazhen
 * @since 2023-09-11
 */
@RestController
@RequestMapping("/api/user")
public class UserController {

    @Autowired
    private IUserService userService;

    @ApiOperation("账号是否存在")
    @GetMapping("/existId/{id}")
    public Result existId(@PathVariable String id){
        boolean existId = userService.existId(id);
        return Result.ok(existId?0:1);
    }

    @ApiOperation("邮箱是否存在")
    @GetMapping("/existEmail/{email}")
    public Result existEmail(@PathVariable String email){
        boolean existEmail = userService.existEmail(email);
        return Result.ok(existEmail?1:0);
    }

    @ApiOperation("用户注册")
    @PostMapping("/register")
    public Result register(@RequestBody User user){
        boolean success = userService.register(user);
        return Result.ok(success?1:0);
    }


    @ApiOperation("重置密码")
    @PostMapping("/retrieve")
    public Result retrievePassword(@RequestBody UserVo userVo){
        boolean success = userService.retrievePassword(userVo);
        return Result.ok(success?1:0);
    }

    @ApiOperation("更新用户信息")
    @PostMapping("/update/{token}")
    public Result updateUser(@RequestBody User user, @PathVariable String token){
        boolean success = userService.updateUser(user,token);
        return Result.ok(success?1:0);
    }
}
